<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Administrador_Model extends CI_Model {
	
	public function getActions() {
		return $this->db->get('user_action')->result();
	}
	
	public function insertProfile($nome, $descricao, $actions) {
		$this->db->insert('user_profile', array(
			'name' => $nome,
			'description' => $descricao
		));
		$profile_id = $this->db->insert_id();
		foreach ($actions as $action) {
			$this->db->insert('user_profile_x_action', array(
				'user_profile_id' => $profile_id,
				'user_action_id' => $action
			));
		}
	}
	
	public function profileExists($id) {
		$profile = $this->db->get_where('user_profile', array('id'=>$id))->row();
		return (count($profile)>0);
	}
	
	public function getProfiles() {
		return $this->db->get('user_profile')->result();
	}
	
	public function getProfile($profile) {
		$this->db->where('id', $profile);
		return $this->db->get('user_profile')->row();
	}
	
	public function getActionsInProfiles($profiles) {
		$this->db->select('user_action_id');
		$this->db->where_in('user_profile_id', $profiles);
		return $this->db->get('user_profile_x_action')->result();
	}

	public function deleteProfile($profile) {
		$this->db->delete('user_profile_x_action', array('user_profile_id' => $profile));
		$this->db->delete('user_x_profile', array('user_profile_id' => $profile));
		$this->db->delete('user_profile', array('id' => $profile));
	}
	
	public function updateProfile($id, $name, $description, $actions) {
		$data = array(
			'name' => $name,
			'description' => $description
		);
		$this->db->where('id', $id);
		$this->db->update('user_profile', $data);
		$this->db->delete('user_profile_x_action', array('user_profile_id' => $id));
		foreach ($actions as $action) {
			$this->db->insert('user_profile_x_action', array(
				'user_profile_id' => $id,
				'user_action_id' => $action
			));
		}
	}

	public function getUsers() {
		return $this->db->get('user')->result();
	}
	
	public function getUser($user) {
		$this->db->where('id', $user);
		return $this->db->get('user')->row();
	}
	
	public function getProfilesInUser($user) {
		$this->db->select('user_profile_id');
		$this->db->where_in('user_id', $user);
		return $this->db->get('user_x_profile')->result();
	}
	
	public function updateUser($id, $data, $perfis) {
		$this->db->where('id', $id);
		$this->db->update('user', $data);
		$this->db->delete('user_x_profile', array('user_id' => $id));
		foreach ($perfis as $perfil) {
			$this->db->insert('user_x_profile', array(
				'user_id' => $id,
				'user_profile_id' => $perfil
			));
		}
	}
	
	public function userExists($id) {
		$user = $this->db->get_where('user', array('id'=>$id))->row();
		return (count($user)>0);
	}
	
	public function deleteUser($user) {
		$this->db->delete('user_x_profile', array('user_id' => $user));
		$this->db->delete('user', array('id' => $user));
	}
	
	public function updateProfileInUser($id, $perfis) {
		$this->db->delete('user_x_profile', array('user_id' => $id));
		foreach ($perfis as $perfil) {
			$this->db->insert('user_x_profile', array(
				'user_id' => $id,
				'user_profile_id' => $perfil
			));
		}
	}
}
